package com.example.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseHelper extends SQLiteOpenHelper
{

	private static final int VERSION = 1;

	public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
	{
		super(context, name, factory, version);
	}

	public DatabaseHelper(Context context, String name)
	{
		this(context, name, VERSION);
	}

	public DatabaseHelper(Context context, String name, int version)
	{
		this(context, name, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		System.out.println("create a Database");
		String SQLStr = null;
		String hanStr = null;
		// User
		SQLStr = "CREATE TABLE FtpParam ( HostName VARCHAR(32) NOT NULL,Port  VARCHAR(32) NOT NULL,UserName VARCHAR(32) NOT NULL,Password  VARCHAR(64) NOT NULL);";
		db.execSQL(SQLStr);

	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		System.out.println("update a Database," + "oldVersion-->" + oldVersion + "newVersion-->" + newVersion);
	}

	public boolean insert(String SQLStr)
	{
		System.out.println("insert:" + SQLStr);
		SQLiteDatabase db = this.getWritableDatabase();
		try
		{
			db.execSQL(SQLStr);
		} catch (Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
		return true;

	}

	public boolean update(String SQLStr)
	{
		System.out.println("update:" + SQLStr);
		SQLiteDatabase db = this.getWritableDatabase();
		try
		{
			db.execSQL(SQLStr);
		} catch (Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
		return true;
	}

	public boolean delete(String SQLStr)
	{
		System.out.println("delete:" + SQLStr);
		SQLiteDatabase db = this.getWritableDatabase();
		try
		{
			db.execSQL(SQLStr);
		} catch (Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
		return true;
	}
	
	
	public Cursor select(String SQLStr)
	{
		System.out.println("select:" + SQLStr);
		SQLiteDatabase db = this.getReadableDatabase();
		return db.rawQuery(SQLStr, null);
	}

	public int getCount(String SQLStr)
	{
		System.out.println("select:" + SQLStr);
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cur = db.rawQuery(SQLStr, null);
		int x = cur.getCount();
		cur.close();
		db.close();
		return x;
	}

}
